package ebiz.buddhist.texts;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import ebiz.buddhist.texts.network.NetworkUtil;

public class Main extends Activity{

// <-- sekisuri 12/04/16 add selected layout		    
	

	private GridView gv;
	//public String[] intentEx = {"chunsu.mp3","heartsutra.mp3","sinju.mp3","beopseong.mp3","chunji.mp3",
	//		"semin_hwaeom.mp3","semin_valwon.mp3","ami.mp3"};
	public String[] intentEx = {
			"chunsu.mp3","han_chunsu.mp3",
			"heartsutra.mp3","han_heartsutra.mp3",
			"sinju.mp3","beopseong.mp3",
			"chunji.mp3","diamond_sutra.mp3",
			"semin_hwaeom.mp3","semin_valwon.mp3",
			"ami.mp3","sinmyo.mp3",
			"yebull.mp3","prajna.mp3"
			};
	
	public String[] strUrl = {
			"file:///android_asset/chunsu.html","file:///android_asset/han_chunsu.html",
			"file:///android_asset/heartsutra.html","file:///android_asset/han_heartsutra.html",
			"file:///android_asset/sinju.html","file:///android_asset/beopseong.html",
			"file:///android_asset/chunji.html","file:///android_asset/diamondsutra.html",
			"file:///android_asset/hwaeom.html","file:///android_asset/valwon.html",
			"file:///android_asset/ami.html","file:///android_asset/sinmyo.html",
			"file:///android_asset/yebull.html","file:///android_asset/prajna.html",
			};
	final static int ACT_BUDTEXT = 0;
	ConnectivityManager cMgr;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);		
		setContentView(R.layout.main);		
		//getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main_titlebar);
				
		gv = (GridView) findViewById(R.id.grid);
		ImagesAdapter imagesAdapter = new ImagesAdapter(this);
		gv.setAdapter(imagesAdapter);		
	
		gv.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> parent,View view, int position,long id){
				mOnClick(position);				
			}
		});	
	//	initView(); // sekisuri 12/04/16
	}
	public void CommAct(String exput,int position)
	{
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(exput),
                Main.this, BuddhistTextsView.class);
		intent.putExtra("TextIn",intentEx[position]);
		startActivity(intent);
	}
	public String androidVersion()
   {
	   String strSoftwareVersion = android.os.Build.VERSION.RELEASE;
	   return strSoftwareVersion;
	   
   }
   public boolean isVersion()
   {
	   String strCheck = androidVersion();
	   int compare = Integer.parseInt(strCheck.substring(0,1));
	   int compare2 =Integer.parseInt(strCheck.substring(2,3));
	   if(compare < 2)
	   {
		   return false;
	   }
	   else
	   {
		   if(compare2 > 2)
		   {
			   return true;
		   }
	   }
	   
	   return false;
   }
	public void mOnClick(int pos)
	{
		switch(pos)
		{
			case 0:
				//startActivity(new Intent(Main.this,PhoneGapActivity.class));
				CommAct(strUrl[pos],pos);
				break;
			case 1:
				CommAct(strUrl[pos],pos);
				break;
			case 2:
				CommAct(strUrl[pos],pos);
				break;
			case 3:
				CommAct(strUrl[pos],pos);			
				break;		
			case 4:
				CommAct(strUrl[pos],pos);
				break;
			case 5:
				CommAct(strUrl[pos],pos);
				break;
			case 6:
				CommAct(strUrl[pos],pos);
				break;
			case 7:
				CommAct(strUrl[pos],pos);
				break;
			case 8:
				CommAct(strUrl[pos],pos);
				break;
			case 9:
				CommAct(strUrl[pos],pos);
				break;
			case 10:
				CommAct(strUrl[pos],pos);			
				break;		
			case 11:
				CommAct(strUrl[pos],pos);		
			case 12:
				CommAct(strUrl[pos],pos);
				break;
			case 13:
				CommAct(strUrl[pos],pos);
				break;
		
			case 14:
	/* sekisuri 20140330			
				Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("file:///android_asset/jijang/jijang01.html"),
		                Main.this, JiJangView.class);
				intent.putExtra("TextIn","jijang01.mp3");
				startActivity(intent);
	*/			
				Intent intentJijang = new Intent(Intent.ACTION_VIEW, Uri.parse("file:///android_asset/jijang/jijang_main.html"),this, JijangViewNew.class);
				startActivity(intentJijang);
				break;
			/*case 15:
			
				Intent intent_Diamond = new Intent(Intent.ACTION_VIEW, Uri.parse("file:///android_asset/diamondsutra.html"),
						Main.this,BuddhistTextsView.class);
				intent_Diamond.putExtra("TextIn","diamond_sutra.mp3");
				startActivity(intent_Diamond);
				break;*/
				
			case 15:
				cMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
				if (NetworkUtil.connectionPresent(cMgr))
				{
					startActivity(new Intent(Main.this, DownAndDelManager.class));
				}
				else
				{
					Toast.makeText(Main.this, getString(R.string.networkerror), Toast.LENGTH_LONG).show();
				}
				break;				
				
			case 16:
				startActivity(new Intent(Main.this,AudioPlay.class));
				break;
				
		
			case 17:
				startActivity(new Intent(Main.this, CountMain.class));	
				break;
				
			
			case 18:
				Intent intentAbout = new Intent(Intent.ACTION_VIEW, Uri.parse("file:///android_asset/about.html"),this, About.class);
				startActivity(intentAbout);
				break;
		}
	}
	public class ImagesAdapter extends BaseAdapter {
    	
    	private final LayoutInflater mInflater;
    	private final ArrayList<ListItem> mItems = new ArrayList<ListItem>();
    	
    	public class ListItem {     
    	//	public final CharSequence text;
    	//	public final CharSequence url;
    		public final Drawable image;
    		public final Drawable touchimage;
    		
    	// sekisuri 20140618	
    	//public ListItem(Resources res, int textResourceId, int urlResourceId, int imageResourceId, int touchImageResourceId) {
    			//text = res.getString(textResourceId);
    			//url = res.getString(urlResourceId);
    	   public ListItem(Resources res,int imageResourceId,int touchImageResourceId) {
    			if(imageResourceId != -1) {
    				image = res.getDrawable(imageResourceId);
    			} else {
    				image = null;
    			}    			
    			if(touchImageResourceId != -1) {
    				touchimage = res.getDrawable(touchImageResourceId);
    			} else {
    				touchimage = null;
    			}
    		}
    	}
    	
	public ImagesAdapter(Main launcher) {    		
		super();    		
		mInflater = (LayoutInflater) launcher.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		// Create default actions
		Resources res = launcher.getResources();
		
		mItems.add(new ListItem(res, R.drawable.item_chunsu, R.drawable.item_chunsu));
		//천수경  1
		mItems.add(new ListItem(res,  R.drawable.item_hanchunsu, R.drawable.item_hanchunsu));
		//우리말 천수경 2
		mItems.add(new ListItem(res, R.drawable.item_heart, R.drawable.item_heart));
		//반야심경 3
		mItems.add(new ListItem(res,  R.drawable.item_hanheart, R.drawable.item_hanheart));
		//우리말 반야심경 4 
		mItems.add(new ListItem(res,R.drawable.item_sinju, R.drawable.item_sinju));
		//대불정 능엄신주 5
		mItems.add(new ListItem(res, R.drawable.item_beopseong, R.drawable.item_beopseong));
		//법성게  6
		mItems.add(new ListItem(res, R.drawable.item_chunji, R.drawable.item_chunji));
		//천지팔양 신주경 7
		mItems.add(new ListItem(res, R.drawable.item_diamond, R.drawable.item_diamond));
		//금강경 8
		mItems.add(new ListItem(res, R.drawable.item_hwaeom, R.drawable.item_hwaeom));
		//이산혜연선사 발원문 9
		mItems.add(new ListItem(res,  R.drawable.item_valwon, R.drawable.item_valwon));
		// 화엄경 10
		mItems.add(new ListItem(res, R.drawable.item_ami, R.drawable.item_ami));
		// 아미타경 11
		mItems.add(new ListItem(res,  R.drawable.item_sinmyo, R.drawable.item_sinmyo));
		//신묘장구대다다리니 12
		mItems.add(new ListItem(res, R.drawable.item_yebull, R.drawable.item_yebull));
		// 예불문 13
		mItems.add(new ListItem(res,  R.drawable.item_sanskrit, R.drawable.item_sanskrit));
		// 산크리트 반야심경 14
		mItems.add(new ListItem(res, R.drawable.item_jijang, R.drawable.item_jijang));
		// 지장경 15
	
		mItems.add(new ListItem(res,  R.drawable.item_downmanager, R.drawable.item_downmanager));
		// 다운삭제 16 
		mItems.add(new ListItem(res, R.drawable.item_music, R.drawable.item_music));
		// 독경 듣기 17
		mItems.add(new ListItem(res, R.drawable.item_108, R.drawable.item_108));
		// 108배 18
	
		mItems.add(new ListItem(res, R.drawable.item_help2, R.drawable.item_help2));
		// 도움말 19
	/*	
		public String[] intentEx = {
				"chunsu.mp3","han_chunsu.mp3",
				"heartsutra.mp3","han_heartsutra.mp3",
				"sinju.mp3","beopseong.mp3",
				"chunji.mp3","diamond_sutra.mp3",
				"semin_hwaeom.mp3","semin_valwon.mp3",
				"ami.mp3","sinmyo.mp3",
				"yebull.mp3","prajna.mp3"
				};
	
		
		mItems.add(new ListItem(res, R.string.item_chunsu, R.string.item_chunsu, R.drawable.item_chunsu, R.drawable.item_chunsu));
		//천수경  1
		mItems.add(new ListItem(res, R.string.item_kor_chunsu, R.string.item_kor_chunsu, R.drawable.item_hanchunsu, R.drawable.item_hanchunsu));
		//우리말 천수경 2
		mItems.add(new ListItem(res, R.string.item_sutra, R.string.item_sutra, R.drawable.item_heart, R.drawable.item_heart));
		//반야심경 3
		mItems.add(new ListItem(res, R.string.item_kor_heartsutra, R.string.item_kor_heartsutra, R.drawable.item_hanheart, R.drawable.item_hanheart));
		//우리말 반야심경 4 
		mItems.add(new ListItem(res, R.string.item_sinju, R.string.item_sinju, R.drawable.item_sinju, R.drawable.item_sinju));
		//대불정 능엄신주 5
		mItems.add(new ListItem(res, R.string.item_beopseong, R.string.item_beopseong, R.drawable.item_beopseong, R.drawable.item_beopseong));
		//법성게  6
		mItems.add(new ListItem(res, R.string.item_chunji, R.string.item_chunji, R.drawable.item_chunji, R.drawable.item_chunji));
		//천지팔양 신주경 7
		mItems.add(new ListItem(res, R.string.item_diamond, R.string.item_diamond, R.drawable.item_diamond, R.drawable.item_diamond));
		//금강경 8
		mItems.add(new ListItem(res, R.string.item_haum, R.string.item_haum, R.drawable.item_hwaeom, R.drawable.item_hwaeom));
		//이산혜연선사 발원문 9
		mItems.add(new ListItem(res, R.string.item_valwon, R.string.item_valwon, R.drawable.item_valwon, R.drawable.item_valwon));
		// 화엄경 10
		mItems.add(new ListItem(res, R.string.item_ami, R.string.item_ami, R.drawable.item_ami, R.drawable.item_ami));
		// 아미타경 11
		mItems.add(new ListItem(res, R.string.item_sinmo , R.string.item_sinmo , R.drawable.item_sinmyo, R.drawable.item_sinmyo));
		//신묘장구대다다리니 12
		mItems.add(new ListItem(res, R.string.item_yebull, R.string.item_yebull, R.drawable.item_yebull, R.drawable.item_yebull));
		// 예불문 13
		mItems.add(new ListItem(res, R.string.item_prajna2, R.string.item_prajna2, R.drawable.item_sanskrit, R.drawable.item_sanskrit));
		// 산크리트 반야심경 14
		mItems.add(new ListItem(res, R.string.item_jijang, R.string.item_jijang, R.drawable.item_jijang, R.drawable.item_jijang));
		// 지장경 15
	
		mItems.add(new ListItem(res, R.string.item_down, R.string.item_down, R.drawable.item_downmanager, R.drawable.item_downmanager));
		// 다운삭제 16 
		mItems.add(new ListItem(res, R.string.item_music, R.string.item_music, R.drawable.item_music, R.drawable.item_music));
		// 독경 듣기 17
		mItems.add(new ListItem(res, R.string.item_bows, R.string.item_bows, R.drawable.item_108, R.drawable.item_108));
		// 108배 18
	
		mItems.add(new ListItem(res, R.string.item_readme, R.string.item_readme, R.drawable.item_help2, R.drawable.item_help2));
		// 도움말 19
		 
		 */
	}
	
   	    	
    	public View getView(int position, View convertView, ViewGroup parent) {
    		final ListItem item = (ListItem) getItem(position);
    		if(convertView == null) {
    			convertView = mInflater.inflate(R.layout.main_icon, parent, false);
    		}
    		
    		final TextView textView = (TextView) convertView;
    		textView.setTag(item);
 // sekisuri 20140618   		textView.setText(item.text);
     		textView.setCompoundDrawablesWithIntrinsicBounds(null, item.image, null, null);
    	/*	
     		textView.setOnTouchListener(new OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					
					// TODO Auto-generated method stub
					if(event.getAction() == MotionEvent.ACTION_DOWN) {
						//Log.d("Down", MotionEvent.ACTION_DOWN+"");
						textView.setCompoundDrawablesWithIntrinsicBounds(null, item.touchimage, null, null);
					} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
						//Log.d("Move", event.getAction()+"");
						textView.setCompoundDrawablesWithIntrinsicBounds(null, item.touchimage, null, null);	
					} else if (event.getAction() == MotionEvent.ACTION_UP) {
					
						Toast.makeText(Main.this,mItems.getItemid() +"번",Toast.LENGTH_SHORT).show();
						//	startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("file:///android_asset/chunsu.html"),
			    //                 Main.this, Chonsu.class));
					} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
						//Log.d("Outside", event.getAction()+"");
						textView.setCompoundDrawablesWithIntrinsicBounds(null, item.image, null, null);					
					} else if (event.getAction() != MotionEvent.ACTION_DOWN) {
						//Log.d("NotDown", event.getAction()+"");
						textView.setCompoundDrawablesWithIntrinsicBounds(null, item.image, null, null);
					}					
					return true;
				}
			});
     	*/	
    		return convertView;
    	}
    	

		public int getCount() {
    		return mItems.size();
    	}
    	
    	public Object getItem(int position) {
    		return mItems.get(position);
    	}
    	
    	public long getItemId(int position) {
    		return position;
    	}
    }
	 @Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
	  if(event.getAction() == KeyEvent.ACTION_DOWN){
	   if(keyCode == KeyEvent.KEYCODE_BACK){
		   alertDown();
		   return true;
	   }
	  }
	  return false;
	 }
	 public boolean onCreateOptionsMenu(Menu menu) {
			super.onCreateOptionsMenu(menu);
			menu.add(0,1,0,getString(R.string.menu_exit));
			return true;
		}
		public boolean onOptionsItemSelected(MenuItem item) {
			switch (item.getItemId()) {
			case 1:	
				alertDown();
				return true;
			}		
			return false;
		}
	 void alertDown()
	{
		new AlertDialog.Builder(Main.this)
		.setTitle(getString(R.string.dialog_alert))
		.setMessage(getString(R.string.dialog_exit))
		.setPositiveButton(getString(R.string.dialog_yes), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {			
				finish();				
			}
		})				
		.setNegativeButton(getString(R.string.dialog_no), null)
		.show();
	}
	 
}
